package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.service.CourseTrainCommonService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DescribeException;
import com.dingreading.cloud.common.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RefreshScope
@RestController
@RequestMapping("/system/courseTrain")
@Api(tags = "总部-学院线下培训课程管理")
public class CourseTrainController {
    @Resource
    private CourseTrainCommonService courseTrainCommonService;

    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "活动名") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "主分类Uid") @RequestParam(value = "mainTypeUid") String mainTypeUid,
            @ApiParam(value = "关联分类Uid") @RequestParam(value = "typeUid") String typeUid,
            @ApiParam(value = "启用状态 1.启用 0.禁用") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        return courseTrainCommonService.pageList(pageUtil, headStaffUid, headStaffName, keyword, mainTypeUid, typeUid, enabled);
    }

    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "新增/修改")
    @PostMapping(value = "/newBean")
    public R<Object> newBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "课程Uid") @RequestParam(value = "uid", required = false) String uid,
            @ApiParam(value = "课程名称") @RequestParam(value = "name") String name,
            @ApiParam(value = "内容介绍") @RequestParam(value = "intro", required = false) String intro,
            @ApiParam(value = "主分类Uid") @RequestParam(value = "mainTypeUid") String mainTypeUid,
            @ApiParam(value = "关联分类Uid") @RequestParam(value = "typeUid") String typeUid
    ) {
        return courseTrainCommonService.newBean(headStaffUid, headStaffName, uid, name, intro, mainTypeUid, typeUid);
    }

    @ApiOperation(value = "根据UID获取信息")
    @PostMapping(value = "/getByUid", produces = "application/json; charset=UTF-8")
    public R<Object> getByUid(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "课程Uid") @RequestParam(value = "uid") String uid
    ) {
        return courseTrainCommonService.getByUid(headStaffUid, headStaffName, uid);
    }

    @ApiOperation(value = "更新启用状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "课程Uid") @RequestParam(value = "uid") String uid,
            @ApiParam(value = "状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        return courseTrainCommonService.updateEnabled(headStaffUid, headStaffName, uid, enabled);
    }

    @ApiOperation(value = "上移/下移")
    @PostMapping(value = "/upOrDown", produces = "application/json; charset=UTF-8")
    public R<Object> upOrDown(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "课程Uid") @RequestParam(value = "uid") String uid,
            @ApiParam(value = "类型 1.上移 2.下移") @RequestParam(value = "type") Integer type
    ) {
        return courseTrainCommonService.upOrDown(headStaffUid, headStaffName, uid, type);
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "课程Uid") @RequestParam(value = "uid") String uid
    ) {
        return courseTrainCommonService.dropBean(headStaffUid, headStaffName, uid);
    }

    @ApiOperation(value = "获取观看地址")
    @PostMapping(value = "/getCdnUrl", produces = "application/json; charset=UTF-8")
    public R<String> getCdnUrl(
            HttpServletRequest request,
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "文件地址") @RequestParam(value = "url") String url
    ) {
        return courseTrainCommonService.getCdnUrl(request, headStaffUid, headStaffName, url);
    }
}
